package com.zzyang.mybatis.jdbc.loadbalance.factory;

import com.zzyang.mybatis.jdbc.config.IConstants;
import com.zzyang.mybatis.jdbc.factory.ExtensionFactory;
import com.zzyang.mybatis.jdbc.factory.SpringExtensionFactory;
import com.zzyang.mybatis.jdbc.loadbalance.LoadBalance;
import com.zzyang.mybatis.jdbc.loadbalance.RandomLoadBalance;
import com.zzyang.mybatis.jdbc.loadbalance.RoundRobinLoadBalance;
import org.springframework.util.StringUtils;

/**
 * 默认的负载均衡工厂
 *
 * @author zhuoziyang
 * @date 2021/4/8 23:14
 */
public class DefaultLoadBalanceFactory implements LoadBalanceFactory {

    public static final DefaultLoadBalanceFactory INSTANCE = new DefaultLoadBalanceFactory();

    private ExtensionFactory extensionFactory = SpringExtensionFactory.INSTANCE;

    @Override
    public LoadBalance getLoadBalance(String loadBalanceType) {
        if (!StringUtils.hasText(loadBalanceType) || IConstants.RANDOM_LOAD_BALANCE.equals(loadBalanceType)) {
            return new RandomLoadBalance();
        }
        if (IConstants.ROUND_ROBIN_LOAD_BALANCE.equals(loadBalanceType)) {
            return new RoundRobinLoadBalance();
        }

        return extensionFactory.getInstance(loadBalanceType, LoadBalance.class);
    }

}
